home *** CD-ROM | disk | FTP | other *** search
- /* printf3.c, from p. 451 of Turbo C Bible */
- #include <stdio.h>
- char far *strf = "Far string...";
- char near *strn = "Near string...";
- char *var_name[] = {"long_name_variable", "shorter_var", "short"};
- double values[] = {1.23, 3.4567, 9.87654321};
- unsigned int num_vars = sizeof(values)/sizeof(double);
- main()
- {
- int i, j, numprint, chcount, width, precision = 0;
- numprint = printf("Some special features of printf\n%n", &chcount);
- printf("printf returned %d and character count in "
- " variable is %d\n", numprint, chcount);
- /* Use of addressing mode modifiers */
- printf("\nYou can print 'near' and 'far' data items properly:\n");
- printf("Example: %Fs (far),\n%Ns (near) will print in any model\n",
- strf, strn);
- /* Printing addresses of variables */
- printf("\nYou can even print the addresses:\n");
- printf("Item Segment:Offset\n");
- printf("'far' string: %Fp\n", (void far *)strf);
- printf("'near' string: %Fp\n", (void far *)strn);
- /* Width and precision can be decided */
- /* at run-time */
- printf("\nThe format can even be decided at run-time\n");
- for (i = 0; i < num_vars; i++)
- {
- /* Find maximum length of variable names */
- if ((j = strlen(var_name[i])) > precision)
- precision = j;
- }
- /* Make the width 4 characters longer */
- /* and print names left justified */
- width = precision + 4;
- printf("--- Table of Variables ---\n");
- for (i = 0; i < num_vars; i++)
- {
- printf("%-*.*s %12.8f\n", width, precision, var_name[i],
- values[i]);
- }
- }